//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MarketModelMultiProduct.h"
using namespace Cephei::QL::Models::Marketmodels;
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProductCashFlow.h>
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::CMarketModelMultiProduct (boost::shared_ptr<QuantLib::MarketModelMultiProduct>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelMultiProduct = NULL;
#endif
	_ppMarketModelMultiProduct = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::CMarketModelMultiProduct (QuantLib::MarketModelMultiProduct& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMarketModelMultiProduct = NULL;
#endif
	_ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (&childNative);
    
    _MarketModelMultiProductOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::CMarketModelMultiProduct (CMarketModelMultiProduct^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelMultiProduct = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::CMarketModelMultiProduct (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelMultiProduct = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelMultiProduct::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::CMarketModelMultiProduct (QuantLib::Handle<QuantLib::MarketModelMultiProduct>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelMultiProduct = &childNative;
	_ppMarketModelMultiProduct = &static_cast<boost::shared_ptr<QuantLib::MarketModelMultiProduct>>(childNative.currentLink());
    
    _MarketModelMultiProductOwner = owner;
}
Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::CMarketModelMultiProduct (QuantLib::Handle<QuantLib::MarketModelMultiProduct> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelMultiProduct = &childNative;
	_ppMarketModelMultiProduct = &static_cast<boost::shared_ptr<QuantLib::MarketModelMultiProduct>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::CMarketModelMultiProduct (QuantLib::MarketModelMultiProduct childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelMultiProduct = NULL;
#endif
	_ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (new QuantLib::MarketModelMultiProduct (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::~CMarketModelMultiProduct ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMarketModelMultiProduct != NULL)
    {
	    delete _ppMarketModelMultiProduct;
        _ppMarketModelMultiProduct = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::!CMarketModelMultiProduct ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMarketModelMultiProduct != NULL)
    {
	    delete _ppMarketModelMultiProduct;
    }
}
QuantLib::MarketModelMultiProduct& Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::GetReference ()
{
    if (_ppMarketModelMultiProduct == NULL) throw REFNEW NativeNullException ();
	return **_ppMarketModelMultiProduct;
}
boost::shared_ptr<QuantLib::MarketModelMultiProduct>& Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::GetShared ()
{
    if (_ppMarketModelMultiProduct == NULL) throw REFNEW NativeNullException ();
	return *_ppMarketModelMultiProduct;
}
QuantLib::MarketModelMultiProduct* Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::GetPointer ()
{
    if (_ppMarketModelMultiProduct == NULL) throw REFNEW NativeNullException ();
	return &**_ppMarketModelMultiProduct;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelMultiProduct>& Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::GetHandle ()
{
	if (_phMarketModelMultiProduct == NULL)
	{
		_phMarketModelMultiProduct = new Handle<QuantLib::MarketModelMultiProduct> (*_ppMarketModelMultiProduct);
	}
	return *_phMarketModelMultiProduct;
}
#endif
bool Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::HasNative () 
{
	return (_ppMarketModelMultiProduct != NULL);
}

Boolean Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::NextTimeStep (Cephei::QL::Models::Marketmodels::ICurveState^ currentState, Cephei::Core::IVector<UInt64>^ numberCashFlowsThisStep, Cephei::Core::IMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ cashFlowsGenerated)
{
    CCurveState^ _CcurrentState;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CcurrentState = safe_cast<CCurveState^> (currentState);
        _CcurrentState->Lock();
        QuantLib::CurveState& _currentState = static_cast<QuantLib::CurveState&> (_CcurrentState->GetReference ()); 
        numberCashFlowsThisStep->Lock();
        INativeVector<UInt64>^ _NCInumberCashFlowsThisStep = numberCashFlowsThisStep->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCnumberCashFlowsThisStep = safe_cast<CUInt64Vector^>(_NCInumberCashFlowsThisStep);
        std::vector<QuantLib::Size>& _numberCashFlowsThisStep = static_cast<std::vector<QuantLib::Size>&> (_NCnumberCashFlowsThisStep->GetReference ());
        cashFlowsGenerated->Lock();
        INativeMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ _NCIcashFlowsGenerated = cashFlowsGenerated->getFeature (NativeFeature::Value);
        CMarketModelMultiProductCashFlowMatrix^ _NCcashFlowsGenerated = safe_cast<CMarketModelMultiProductCashFlowMatrix^>(_NCIcashFlowsGenerated);
        std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >& _cashFlowsGenerated = static_cast<std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >&> (_NCcashFlowsGenerated->GetReference ());
    	bool _rv = (bool)(*_ppMarketModelMultiProduct)->nextTimeStep ( _currentState,  _numberCashFlowsThisStep,  _cashFlowsGenerated );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentState != nullptr) _CcurrentState->Unlock();
        if (numberCashFlowsThisStep != nullptr) numberCashFlowsThisStep->Unlock(); //NO2  
        if (cashFlowsGenerated != nullptr) cashFlowsGenerated->Unlock(); //NO2  
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppMarketModelMultiProduct)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::MaxNumberOfCashFlowsPerProductPerStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelMultiProduct)->maxNumberOfCashFlowsPerProductPerStep ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::NumberOfProducts::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelMultiProduct)->numberOfProducts ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppMarketModelMultiProduct)->possibleCashFlowTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IMarketModelMultiProduct^ Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppMarketModelMultiProduct)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct::SuggestedNumeraires::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppMarketModelMultiProduct)->suggestedNumeraires ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

